package com.hrb.douban.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hrb.douban.CustomApplication;
import com.hrb.douban.OnItemClickListener;
import com.hrb.douban.R;
import com.hrb.douban.record.DirectorRecord;
import com.hrb.douban.utils.ImageUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DirectorAdapter extends RecyclerView.Adapter<DirectorAdapter.ViewHolder> {
    private List<DirectorRecord> directors;
    private OnItemClickListener mClickListener;

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.director_recyclerview, viewGroup, false);
        ViewHolder vholder = new ViewHolder(view, mClickListener);
        return vholder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        DirectorRecord directorRecord=directors.get(i);
        viewHolder.itemDirectorName.setText(directorRecord.getName());
        viewHolder.itemDirectorRole.setText(directorRecord.getRole());
        Glide.with(CustomApplication.getContext()).load(directorRecord.getAvatars()).override(270, 383).centerCrop().into(viewHolder.itemDirectorImg);
        //ImageUtil.loadIntoUseFitWidth(directorRecord.getAvatars(),viewHolder.itemDirectorImg);
    }

    public DirectorAdapter(List<DirectorRecord> directors) {
        this.directors = directors;
    }

    public DirectorAdapter(List<DirectorRecord> directors,OnItemClickListener mClickListener) {
        this.mClickListener = mClickListener;
        this.directors = directors;
    }

    @Override
    public int getItemCount() {
        return directors.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @BindView(R.id.item_director_img)
        ImageView itemDirectorImg;
        @BindView(R.id.item_director_name)
        TextView itemDirectorName;
        @BindView(R.id.item_director_role)
        TextView itemDirectorRole;
        private OnItemClickListener mListener;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public ViewHolder(@NonNull View itemView, OnItemClickListener mListener) {
            super(itemView);
            this.mListener = mListener;
            itemView.setOnClickListener(this);
            ButterKnife.bind(this, itemView);
        }

        @Override
        public void onClick(View v) {
            if(mListener!=null){
                mListener.onItemClick(v,getLayoutPosition());
            }
        }
    }
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mClickListener = listener;
    }

    public List<DirectorRecord> getDirectors() {
        return directors;
    }
}
